home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / strategy / tictac2-.000 / tictac2- / tictac2 / screen.c < prev    next >
C/C++ Source or Header  |  1980-01-04  |  9KB  |  343 lines

  1. /*****************************************************************************.
  2. **  TicTac2:  Simple TicTacToe two-player game against another user or cpu.  **
  3. **  Copyright (c) 1995 Ian Singh                                             **
  4. **                                                                           **
  5. **  This program is free software; you can redistribute it and/or modify it  **
  6. **  under the terms  of the  GNU General Public License as published by the  **
  7. **  Free Software Foundation;  either version 2 of the License, or (at your  **
  8. **  option) any version later.                                               **
  9. **                                                                           **
  10. **  This program is distriubted in the hope that it  will  be  useful,  but  **
  11. **  WITHOUT   ANY   WARRANTY;   without   even   the  implied  warranty  of  **
  12. **  MERCHANTABILITY or FITNESS FOR  A  PARTICULAR  PURPOSE.   See  the  GNU  **
  13. **  General Public License for more details.                                 **
  14. **                                                                           **
  15. **  You should have received a copy of the GNU General Public License along  **
  16. **  with  this  program;   if not,  write  to the Free Software Foundation,  **
  17. **  Inc.,  675 Mass Ave, Cambridge, MA 02139, USA.                           **
  18. **                                                                           **
  19. **  Ian Singh                           Ian Singh                            **
  20. **  am256@freenet.carleton.ca           3G Arnold Dr.                        **
  21. **                                      Nepean, Ontario                      **
  22. **                                      K2H 6V6                              **
  23. .*****************************************************************************/
  24.  
  25.  
  26. /* +-----------------------------------------------------------------------+ **
  27. ** |  screen.c:                                | **
  28. ** |                                                                       | **
  29. ** |  Some screen/curses routines for my game.                 | **
  30. ** +-----------------------------------------------------------------------+ */
  31.  
  32. #include <ncurses/ncurses.h>
  33. #include <string.h>
  34. #include "tictac2.h"
  35. #include "screen.h"
  36. #include <unistd.h>
  37. #include <ctype.h>
  38.  
  39. char  BOARD[] =
  40. ".---------+---------+---------,"
  41. "|         |         |         |"
  42. "|         |         |         |"
  43. "|         |         |         |"
  44. "+---------+---------+---------+"
  45. "|         |         |         |"
  46. "|         |         |         |"
  47. "|         |         |         |"
  48. "+---------+---------+---------+"
  49. "|         |         |         |"
  50. "|         |         |         |"
  51. "|         |         |         |"
  52. "`---------+---------+---------'";
  53.  
  54. pos pos1 = { SPACE1 };
  55. pos pos2 = { SPACE2 };
  56. pos pos3 = { SPACE3 };
  57. pos pos4 = { SPACE4 };
  58. pos pos5 = { SPACE5 };
  59. pos pos6 = { SPACE6 };
  60. pos pos7 = { SPACE7 };
  61. pos pos8 = { SPACE8 };
  62. pos pos9 = { SPACE9 };
  63.  
  64. int hascolour;
  65.  
  66. /*
  67. **  writex(x,y):  draw an X somewhere.
  68. **
  69. */
  70. void writex(int x, int y)
  71. {
  72.   mvaddch(y,x,'\\');            /* that's a \, stupid */
  73.   mvaddch(y,x+4,'/');
  74.   mvaddch(y+1,x+2,'x');
  75.   mvaddch(y+2,x,'/');
  76.   mvaddch(y+2,x+4,'\\');
  77. }
  78.  
  79. /*
  80. **  writeo(x,y):  draw an O somewhere.
  81. **
  82. */
  83. void writeo(int x,int y)
  84. {
  85.   mvaddch(y,x,'/');
  86.   mvaddch(y,x+1,'~');
  87.   mvaddch(y,x+2,'~');
  88.   mvaddch(y,x+3,'~');
  89.   mvaddch(y,x+4,'\\');
  90.   mvaddch(y+1,x,'|');
  91.   mvaddch(y+1,x+4,'|');
  92.   mvaddch(y+2,x,'\\');
  93.   mvaddch(y+2,x+1,'_');
  94.   mvaddch(y+2,x+2,'_');
  95.   mvaddch(y+2,x+3,'_');
  96.   mvaddch(y+2,x+4,'/');
  97. }
  98.  
  99. /*
  100. **  setup_terminal_sutff(); Init/Setup/Draw the terminal screen.
  101. **
  102. */
  103. void setup_terminal_stuff()  {
  104.   char s;
  105.   int i=0,p=0;
  106.  
  107.   initscr();
  108.   refresh();
  109.   keypad(stdscr,TRUE);
  110.   nocrmode();
  111.   noecho();
  112.   cbreak();
  113.   for ( i=0;i<BOARD_SIZE;i++ )  {
  114.     addch( BOARD[i] );
  115.     if ( p==BOARD_WIDTH )  { addch('\n'); p=0; }  else { p++; }
  116.   }
  117.   mvaddstr(3,35,"TicTac2");
  118.   attrset(A_REVERSE);
  119.   mvaddstr(4,35,"Version 0.6");
  120.   attrset(A_STANDOUT);
  121.   mvaddstr(5,35,"--------------------");
  122.   mvaddstr(10,35,"Author:");
  123.   mvaddstr(11,35,"Ian Singh (am256@freenet.carleton.ca)");
  124.   
  125.   attrset(A_NORMAL);
  126. }
  127.  
  128. /*
  129. **
  130. ** void kill_terminal_stuff(): end all this stuff properly.
  131. */
  132. void kill_terminal_stuff() {
  133.  clear();
  134.   endwin();
  135. }
  136.  
  137. /*
  138. **  HiLiteSpace(pos):  HiLiteASpace by (screen)co-ordinates.
  139. **
  140. */
  141. void HiLiteSpace( pos cur ) {
  142.   int i;
  143.  
  144.   attrset(A_STANDOUT);
  145.   for (i=cur.x;i<(cur.x+11);i++)  {
  146.     mvaddch( cur.y, i,   BOARD[ (((BOARD_WIDTH+1)*cur.y)+i) ]  );
  147.     mvaddch( (cur.y+4), i, BOARD[ (((BOARD_WIDTH+1)*(cur.y+4))+i) ]  );
  148.   }
  149.   for (i=cur.y+1;i<(cur.y+4);i++)  {
  150.     mvaddch( i, cur.x,  BOARD[  (((BOARD_WIDTH+1)*i)+cur.x) ] );
  151.     mvaddch( i, cur.x+10, BOARD[  (((BOARD_WIDTH+1)*i)+(cur.x+10)) ] );
  152.   }
  153.   attrset(A_NORMAL);
  154. }
  155.  
  156. /*
  157. **  UnHiLiteSpace(pos):  UnHiLiteASpace by (screen)co-ordinates.
  158. **
  159. */ 
  160. void UnHiLiteSpace( pos cur ) {
  161.   int i;
  162.  
  163.   attrset(A_NORMAL);
  164.   for (i=cur.x;i<(cur.x+11);i++)  {
  165.     mvaddch( cur.y, i,   BOARD[ (((BOARD_WIDTH+1)*cur.y)+i) ]  );
  166.     mvaddch( (cur.y+4), i, BOARD[ (((BOARD_WIDTH+1)*(cur.y+4))+i) ]  );
  167.   }
  168.   for (i=cur.y+1;i<(cur.y+4);i++)  {
  169.     mvaddch( i, cur.x,  BOARD[  (((BOARD_WIDTH+1)*i)+cur.x) ] );
  170.     mvaddch( i, cur.x+10, BOARD[  (((BOARD_WIDTH+1)*i)+(cur.x+10)) ] );
  171.   }
  172.   attrset(A_NORMAL);
  173. }
  174.  
  175. /*
  176. **  center(x,y):  center *string between x1 and x2 on line y in A_STANDOUT...
  177. **                warning: everything on line y is deleted.
  178. */
  179. void center(int y, int x1, int x2, char *string) {
  180.   move(y,0);
  181.   clrtoeol();
  182.   attrset(A_STANDOUT);
  183.   mvaddstr(y,((x2-x1)/2)-(strlen(string)/2),string);
  184.   attrset(A_NORMAL);
  185. }
  186.  
  187. /*
  188. **  UnHiLiteByNum(int):  another one of those lovely unhilight func's.
  189. **                       but this 'cepts a number instead of co-ordinates
  190. **                       whoop-tee-doo.
  191. */
  192.  
  193. void UnHiLiteByNum(int space)
  194. {
  195.  
  196. switch(space)  {
  197. case 1:  UnHiLiteSpace(pos1);  break;
  198. case 2:  UnHiLiteSpace(pos2);  break;
  199. case 3:  UnHiLiteSpace(pos3);  break;
  200. case 4:  UnHiLiteSpace(pos4);  break;
  201. case 5:  UnHiLiteSpace(pos5);  break;
  202. case 6:  UnHiLiteSpace(pos6);  break;
  203. case 7:  UnHiLiteSpace(pos7);  break;
  204. case 8:  UnHiLiteSpace(pos8);  break;
  205. case 9:  UnHiLiteSpace(pos9);  break;
  206.                }
  207.  
  208. }
  209.  
  210. void WriteXByNum(int space)
  211. {
  212.  
  213. switch(space)  {
  214. case 1:  writex(pos1.x+3, pos1.y+1);  break;
  215. case 2:  writex(pos2.x+3, pos2.y+1);  break;
  216. case 3:  writex(pos3.x+3, pos3.y+1);  break;
  217. case 4:  writex(pos4.x+3, pos4.y+1);  break;
  218. case 5:  writex(pos5.x+3, pos5.y+1);  break;
  219. case 6:  writex(pos6.x+3, pos6.y+1);  break;
  220. case 7:  writex(pos7.x+3, pos7.y+1);  break;
  221. case 8:  writex(pos8.x+3, pos8.y+1);  break;
  222. case 9:  writex(pos9.x+3, pos9.y+1);  break;
  223.                }
  224.  
  225. }
  226.  
  227. void WriteOByNum(int space)
  228. {
  229. switch(space)  {
  230. case 1:  writeo(pos1.x+3, pos1.y+1);  break;
  231. case 2:  writeo(pos2.x+3, pos2.y+1);  break;
  232. case 3:  writeo(pos3.x+3, pos3.y+1);  break;
  233. case 4:  writeo(pos4.x+3, pos4.y+1);  break;
  234. case 5:  writeo(pos5.x+3, pos5.y+1);  break;
  235. case 6:  writeo(pos6.x+3, pos6.y+1);  break;
  236. case 7:  writeo(pos7.x+3, pos7.y+1);  break;
  237. case 8:  writeo(pos8.x+3, pos8.y+1);  break;
  238. case 9:  writeo(pos9.x+3, pos9.y+1);  break;
  239.                }
  240.  
  241. }
  242.  
  243. /*
  244. ** refresh_board():  draw's all the x'S and o'S.
  245. **
  246. */
  247. void refresh_board(char *field) 
  248. {
  249.   int i;
  250.   for ( i=0; i<9; i++ )  {
  251.     switch(field[i])  {
  252.       case 1:  WriteXByNum(i+1); break;
  253.       case 10:  WriteOByNum(i+1); break;
  254.     }
  255.   }
  256.   
  257. }
  258.  
  259. /*
  260. **  HiLiteByNum(int):    another one of those lovely hilight func's.
  261. **                       but this 'cepts a number(1-9) instead of co-ordinates
  262. **                       whoop-tee-doo.
  263. */
  264. void HiLiteByNum(int space)
  265. {
  266.  
  267. switch(space)  {
  268. case 1:  HiLiteSpace(pos1);  break;
  269. case 2:  HiLiteSpace(pos2);  break;
  270. case 3:  HiLiteSpace(pos3);  break;
  271. case 4:  HiLiteSpace(pos4);  break;
  272. case 5:  HiLiteSpace(pos5);  break;
  273. case 6:  HiLiteSpace(pos6);  break;
  274. case 7:  HiLiteSpace(pos7);  break;
  275. case 8:  HiLiteSpace(pos8);  break;
  276. case 9:  HiLiteSpace(pos9);  break;
  277.                }
  278.  
  279. }
  280.  
  281. void Pause()
  282. {  getch(); 
  283.  
  284. /* selectspace(void):  use cursor keys to select a space. q to abort/quit.
  285. ** RETURNS:            if ok, number of space that was picked. if quit, then
  286. **                     I_QUIT(10).
  287. */
  288. int selectspace(char *field)
  289. {
  290.   int key,cs=1;
  291.  
  292.   center(24,0,79,"Use the arrow keys to select a space.");
  293.  
  294.   HiLiteByNum(1);
  295.   while (1)  {
  296.     key=getch();
  297.     switch ( toupper(key) )  {
  298.     case KEY_LEFT: if(cs-1<1) { beep();  center(24,0,79,"can't go that far");}
  299.                      else { UnHiLiteByNum(cs); cs--; HiLiteByNum(cs); } break;
  300.     case KEY_RIGHT: if(cs+1>9) {beep(); center(24,0,79,"can't go that far"); }
  301.                       else { UnHiLiteByNum(cs); cs++; HiLiteByNum(cs); } break;
  302.     case KEY_UP: if(cs-3<1) { beep(); center(24,0,79,"can't go that far");}
  303.                    else { UnHiLiteByNum(cs); cs-=3; HiLiteByNum(cs); } break;
  304.     case KEY_DOWN: if(cs+3>9) {beep(); center(24,0,79,"can't go that far"); }
  305.                    else { UnHiLiteByNum(cs); cs+=3; HiLiteByNum(cs); } break;
  306.     case '\n': if (field[(cs-1)]){beep();center(24,0,79,"already used");}
  307.                else { UnHiLiteByNum(cs); return (cs); } break;
  308.     case 'Q':  return ( I_QUIT );  break;
  309.     default: beep(); center(24,0,79,"Use yer left and right arrowz to move.");
  310.              break;
  311.     }
  312.   }
  313. }
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.